{% extends 'forms/' ~ layout|default('field') ~ '.html.twig' %}

{# If values contains a plain list of items, we need to reindex them. #}
{% set value = not field.key and value is iterable and value|length ? value|values : value %}

{% block field %}
    {% if field.is_current %}
        <div class="g-grid">
            {% for key, val in value %}
                {% set title = (field.value == field.key ? key : val[field.value]) %}
                <div class="card settings-block">
                    <h4>
                        <span data-title-editable="{{ title }}" data-collection-key="{{ (scope ~ '.' ~ key ~ '.' ~ field.value)|fieldName }}" class="title">{{ title }}</span>
                        <i class="fa fa-pencil font-small"  tabindex="0" aria-label="{{ 'GANTRY5_PLATFORM_EDIT_TITLE'|trans|replace({'%s': title}) }}" data-title-edit=""></i>
                    </h4>
                    <div class="inner-params">
                        {{ block('collection_fields') }}
                    </div>
                </div>
            {% endfor %}
        </div>
    {% else %}
        {% set can_reorder = field.reorder ?? true %}
        {% set can_remove = field.deletion ?? true %}
        {% set can_addnew = field.add_new ?? true %}
        <div class="settings-param {{ field.type|replace('.', '-') }}">
            {% if overrideable and (field.overridable is not defined or field.overridable == true or has_value) and field.type != 'container.set' %}
                {% include 'forms/override.html.twig' with {'scope': scope, 'name': name, 'field': field} %}
            {% endif %}
            {% block contents %}
                {% set field_route = (route ~ '.' ~ prefix ~ '.' ~ field.name)|replace('.', '/') %}
                <span class="settings-param-title float-left">
                    {% block label %}
                        {% if field.description %}
                            <span aria-label="{{ field.description }}" data-tip="{{ field.description|raw }}" data-tip-place="top-right">{{ field.label }}</span>
                        {% else %}
                            {{ field.label }}
                        {% endif %}
                        {{ field.validate.required in ['on', 'true', 1] ? '<span class="required">*</span>' }}
                    {% endblock %}
                </span>
                <div class="settings-param-field" data-field-name="{{ field.name }}">
                    {% block input -%}
                        <div class="g5-collection-wrapper">
                        <ul>
                        {%- if field.fields -%}
                                {% for key, val in value %}
                                    {% if (field.ajax == true) %}
                                        <li data-collection-item="{{ field.value }}">
                                            {% set itemValue = field.value == field.key ? key : val[field.value] %}
                                            {% if can_reorder %}<i class="fa fa-reorder font-small item-reorder"></i>{% endif %}
                                            <a class="config-cog" href="{{ gantry.route(field_route ~ '/' ~ key) }}"><span data-title-editable="{{ itemValue }}" class="title">{{ itemValue }}</span></a>
                                            {% if can_remove %}<i class="fa fa-fw fa-trash font-small" data-collection-remove=""></i>{% endif %}
                                            {% if can_addnew %}<i class="fa fa-files-o font-small" data-collection-duplicate=""></i>{% endif %}
                                            <i class="fa fa-fw fa-pencil font-small" tabindex="0" aria-label="{{ 'GANTRY5_PLATFORM_EDIT_TITLE'|trans(itemValue) }}" data-title-edit=""></i>
                                        </li>
                                    {% else %}
                                        {% block collection_fields %}
                                            <div data-g5-collections="">
                                                {% for childName, child in field.fields %}
                                                    {% if childName starts with '.' %}
                                                        {% set childKey = childName|trim('.') %}
                                                        {% set childValue = val[childName[1:]] %}
                                                        {% set childName = name ~ '.' ~ key ~ childName %}
                                                    {% else %}
                                                        {% set childKey = childName %}
                                                        {% set childValue = nested(data, scope ~ childName) %}
                                                        {% set childName = childName|replace({'*': key}) %}
                                                    {% endif %}
                                                    {% if childName not in skip and not child.skip and field.value != childKey %}
                                                         {% if child.type == 'key' %}
                                                             {% include 'forms/fields/key/key.html.twig'
                                                             with {name: childName, field: child, value: key} %}
                                                         {% elseif child.type %}
                                                             {% include ["forms/fields/" ~ child.type|replace('.', '/') ~ ".html.twig", 'forms/fields/unknown/unknown.html.twig']
                                                             with {name: childName, field: child, current_value: childValue, value: null, default_value: null, prefix: (prefix ? prefix ~ '.' : '') ~ childName} %}
                                                        {% endif %}
                                                    {% endif %}
                                                {% endfor %}
                                            </div>
                                        {% endblock %}
                                    {% endif %}
                                {% endfor %}
                        {%- endif -%}
                    </ul>
                    </div>
                    <div>
                        <ul style="display: none">
                            <li data-collection-nosort="" data-collection-template="{{ field.value }}" style="display: none;">
                                {% if can_reorder %}<i class="fa fa-reorder font-small item-reorder"></i>{% endif %}
                                <a class="config-cog" href="{{ gantry.route(field_route ~ '/%id%') }}"><span data-title-editable="New item" class="title">New item</span></a>
                                {% if can_remove %}<i class="fa fa-fw fa-trash font-small" data-collection-remove=""></i>{% endif %}
                                {% if can_addnew %}<i class="fa fa-files-o font-small" data-collection-duplicate=""></i>{% endif %}
                                <i class="fa fa-fw fa-pencil font-small" data-title-edit=""></i>
                            </li>
                        </ul>
                        {% if can_addnew %}<span class="collection-addnew button button-simple" data-collection-addnew="" title="Add new item"><i class="fa fa-plus font-small"></i></span>{% endif %}
                        <a href="{{ gantry.route(field_route) }}" class="collection-editall button button-simple" data-collection-editall="" title="Edit all items" {% if value|length <= 1 %}style="display: none;"{% endif %}><i class="fa fa-th-large font-small"></i></a>
                    </div>
                    <input data-collection-data="" name="{{ (scope ~ name ~ '._json')|fieldName }}" type="hidden" value="{{ value|default({})|json_encode(constant('JSON_UNESCAPED_SLASHES'))|e('html_attr') }}"/>
                    {% endblock %}
                </div>
            {% endblock %}
        </div>
    {% endif %}
{% endblock %}
